<?php
/**
 * 班未军 2015-12-10
 */
namespace backend\controllers;

use Yii;
use backend\controllers\BaseController;
use backend\models\download\UploadForm;
use backend\models\download\Soft;
use backend\models\download\SoftCourse;
use backend\models\download\Softcate;
use backend\models\download\Softlog;
use yii\web\UploadedFile;
use yii\helpers\ArrayHelper;
use yii\base\Object;


/**
 * 下载管理
 */
class DownloadController extends BaseController
{
    /**
     * 软件管理
     */
    public function actionIndex()
    {    
		$model = new Soft;		
        $data  = $model -> PagesTo(); 
        return $this->render('index',[
             'lists' => $data['models'],
             'pages' => $data['pages']
       ]);
    }

    /**
     * 软件上传
     */
    public function actionUploadsoft()
    {
        $request = Yii::$app->request;
        $model = new Soft;
        if (empty($request->post())) {                   
            return $this->render('uploadsoft',$model -> UploadTo());
        }else{
            if($model->load(Yii::$app->request->post()) && $model->validate()) {
                 $cour = new SoftCourse;
                 if($cour->Courseadd($model->Softadd())){
                     $this->success('上传成功！', ['download/index']);
                 }else{
                    $this->error('上传失败！', ['download/index']);
                 }                 
            }else{
                $this->error('非法数据！', ['download/index']);
            }            
        }
    }

    /**
     * curl远程上传软件       暂时不用
     */
	/* private function _Curlupload($filename=''){
		$ch = curl_init();
		//加@符号curl就会把它当成是文件上传处理
		//$data1 = array('soft'=>'@'. dirname(__FILE__).'/img/1.jpg');
		$data = array('soft'=>new \CURLFile('/data0/htdocs/edu/backend/uploads/soft/'.$filename));			
		//print_r($data); die;
		curl_setopt($ch,CURLOPT_URL,"files.bwphp.com/index.php?code=upfile");
		curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
		curl_setopt($ch,CURLOPT_POST,true);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
		$result = curl_exec($ch);
		curl_close($ch);
		return json_decode($result,true);
	 }

	/**
	 * @inheritdoc 删除已上传完的文件
	 
	 private function _Delfile($filename = "") {
	     $file_url = "/data0/htdocs/edu/backend/uploads/soft/".$filename;
	     if(file_exists($file_url)) {
	         unlink($file_url);
	     }
	 }*/
	 
    /**
     * 软件编辑页
     */
    public function actionUpdatesoft()
    {   $model = new Soft;
        if(empty(Yii::$app->request->post())) {		    
            return $this->render('updatesoft',$model->SetsoftData());
        }else{
            if ($model->load(Yii::$app->request->post()) && $model->validate()){
                $cour = new SoftCourse;
                $model->Updsoftto();
                 if($cour->Updsoft()){
                    $this->success('修改成功！', ['download/index']);
                 }else{
                    $this->error('修改失败！', ['download/index']);
                 }
	        }else{
	            $this->error('非法数据！', ['download/index']);
	        }	
        }
    }

    
    /**
     *  软件删除
     */
    public function actionSoftdel()
    {
        $model = new Soft;
	    if($model -> SoftdelData()){
            $this->success('删除成功！', ['download/index']);
	    }else{
            $this->error('删除失败！', ['download/index']);
	    }
    }       

    /**
     *  软件分类页
     */
    public function actionSoftcategory()
    {	$model = new Softcate;
        if(empty(Yii::$app->request->post())){
             return $this->render('softcate',['model' => $model]);
        }else{
             if ($model->load($data = Yii::$app->request->post()) && $model->validate()){
                if($model->Cateadd($data)){
                    $this->success('添加成功！', ['download/softcategory']);
    			}else{
                    $this->error('添加失败！', ['download/softcategory']);
    			}
    	    }else{
                $this->error('非法数据！', ['download/softcategory']);
    		}
        }     
    }


    /**
     * 软件分类列表
     */
    public function actionSoftcatelist()
    {	$model = new Softcate;	
		$lists = $model->Catelist();		
        return $this->render('catelist',['catelist' => $lists['models'], 'pages' => $lists['pages']]);        
    }


    /**
     * 软件分类编辑
     */
    public function actionSoftcateupd()
    {	$model = new Softcate;
        if(empty(Yii::$app->request->post())){
    		$arr = $model -> Cateupd();			
            return $this->render('cateupd',['cate' => $arr,'model' => $model]);   
        }else {
            if($model->load(Yii::$app->request->post()) && $model->validate()) {
                if($model->Cateupdate()) {
                    $this->success('修改成功！', ['download/softcatelist']);
                }else{
                    $this->error('修改失败！', ['download/softcatelist']);
                }
            }else{
                $this->error('非法数据！', ['download/softcatelist']);
            }
        }
    }

    /**
     * 软件分类删除
     */
    public function actionSoftcatedel()
    {
        $model = new Softcate;		
		if($model->Cateupdel()) {
            $this->success('删除成功！', ['download/softcatelist']);
		}else{
            $this->error('删除失败！', ['download/softcatelist']);
		}
    }    

    /**
     * 下载日志
     */
    public function actionLogs()
    {
        $model = new Softlog;
        return $this->render('logs',$model->getLogs(Yii::$app->request->post()));
    }

    
}
